home *** CD-ROM | disk | FTP | other *** search
- // Copyright 1994 A.D. Software. All Rights Reserved
-
- // OOFTEST1
-
- // This sample tests the database backend by creating a single table
- // and storing and retrieving indexed data.
-
- // Simple stream I/O is used to interact with the user.
-
- // NOTE the odd sizes in the fields below are to help trap alignment issues
-
- #include "oofile.hpp"
-
- CLASS_TABLE(dbPeople)
- dbChar LastName, OtherNames;
- dbLong Salary;
- dbText Description;
-
- dbPeople() :
- dbTable("People"),
- LastName(39, "Last Name", kIndexed),
- OtherNames(79, "Other Names", kIndexed),
- Salary("Salary", kIndexed),
- Description("Description")
- {};
-
- // my own data entry procedures
- void Add(const char *lname, const char *oname, const long salary);
- };
-
- void dbPeople::Add(const char *lname, const char *oname, const long salary)
- {
- newRecord();
- LastName = lname;
- OtherNames = oname;
- Salary = salary;
- saveRecord();
- }
-
-
-
- int main()
- {
- cout << "OOFILE Validation Suite - Test 1\n"
- << "Simple test to store some data and retrieve it\n"
- << "in a single-table database, with no relations\n";
-
- dbConnect_ctree theDB;
- dbPeople People;
-
- theDB.useSeparateFiles(); // note the blank connection names!
- // this test creates People.dat, People.idx & Blobs
-
- if (dbConnect::fileExists("People.dat"))
- theDB.openConnection("");
- else {
- theDB.newConnection("");
- People.Add("Smith", "John", 0); // specifically to test zero searches
- People.Description = "John is a plain sort of bloke, not the kind to stand-out in a crowd\n";
- People.Description += "and in fact you'd probably say he's the classic Mr Average. However ";
- People.Description += "he harbours secret dreams of being a brain surgeon and a bloke 'wot ";
- People.Description += "goes down the sewers in big rubber boots'\n";
- People.saveRecord();
-
- People.Add("DENT", "Trissa", 5000);
- People.Description = "Trissa is married to Andy and mother of Tanith and Ryan";
- People.saveRecord();
-
- People.Add("Dent", "Andy", 25000);
- People.Description = "Andy has a Don Quixote complex but is trying to strengthen his Sancho ";
- People.Description += "Panza side to stop him before he gets into trouble,\n rather than taking ";
- People.Description += "Herculean efforts to get him out of the overcommitted messes he creates.\n";
- People.saveRecord();
-
- People.Add("Taylor", "Ken", 75000);
- }
- People.sortBy(People.LastName);
- cout << "Listing records\n" << People << endl;
-
- People.sortBy(People.OtherNames);
- cout << "Listing records in OtherNames order\n" << People << endl;
-
- People.sortBy(People.LastName);
- cout << "Now retrieving by index\n";
- cout << "Retrieving Taylor: " << People["Taylor"].LastName << endl;
-
- cout << "Retrieving Smith by Salary: ";
- People[People.Salary==0];
- cout << People.LastName << endl << endl;
- // save their record number so we can come back to them
- long smithNo = People.recordNumber();
-
- People.search(People.LastName=="Dent");
- cout << "Listing two Dent records: " << endl << People << endl;
-
- cout << endl << "now producing a subset of Taylor & Dent" << endl;
- dbPeople entrepeneurs = People;
- entrepeneurs.search(entrepeneurs.LastName=="Taylor");
- entrepeneurs += People;
- cout << entrepeneurs;
-
- dbPeople savedEnts = entrepeneurs;
- cout << endl << "now reducing via Intersection to Dent" << endl;
- entrepeneurs &= People; // Dent, Taylor & Dent
- cout << entrepeneurs;
-
- cout << endl << "now Inverting to Smith & Taylor" << endl;
- ~entrepeneurs;
- cout << entrepeneurs; // Dent inverted
-
- cout << endl << "now reducing via Difference to Dent" << endl;
- savedEnts -= entrepeneurs; // Dent, Taylor - Smith, Taylor
- cout << savedEnts << endl;
-
- People.selectAll();
-
- // demonstrate going to a specific relative record in a selection
- // then going to the previous record, as would be common in a GUI
- // by double-clicking a line in a browser, then pressing Prev Record
- cout << "Retrieving Taylor by relative record number: ";
- People.gotoRelativeRecord(3);
- cout << People.LastName << endl << endl;
- cout << "Retrieving previous record Smith: ";
- People.prev();
- cout << People.LastName << endl << endl;
-
- // demonstrate going to a specific record in a selection
- cout << "Retrieving Smith by record number: ";
- People.gotoRecord(smithNo);
- cout << People.LastName << endl << endl;
-
- cout << "Test Completed" << endl;
-
- return EXIT_SUCCESS;
- }